package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreMapStatLogic;
import cn.topevery.garden.entity.dto.GreMapStatDTO;
import cn.topevery.garden.entity.oneMap.CountVO;
import cn.topevery.garden.entity.po.GreMapStat;
import cn.topevery.garden.entity.query.GreMapStatQuery;
import cn.topevery.garden.entity.vo.GreMapStatDifferenVo;
import cn.topevery.garden.entity.vo.GreMapStatVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.dal.logic.SysClassLogic;
import hw.topevery.basis.entity.dto.SysClassDto;
import hw.topevery.basis.entity.fetch.ClassFetch;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.locationtech.jts.geom.GeometryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "")
@RestController
@RequestMapping("/greMapStat")
public class GreMapStatController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreMapStatLogic greMapStatLogic;

    @Autowired
    private SysClassLogic sysClassLogic;

    @Autowired
    private GreMapStatLogic greMapStatService;


    private static GeometryFactory factory =new GeometryFactory();

    @GetMapping(value = "/getYears")
    @ApiOperation(value = "获取年份数据")
    public JsonResult<List<CountVO>> getYears() {
        List<CountVO> list = greMapStatService.getYears();
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/getAreaInfo")
    @ApiOperation(value = "获取面积信息")
    public JsonResult<List<Map<String, Object>>> getAreaInfo(Integer year) {
        List<Map<String, Object>> list = greMapStatService.getAreaInfo(year);
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/getDifferenInfo")
    @ApiOperation(value = "获取差异情况")
    public JsonResult<GreMapStatDifferenVo> getDifferenInfo(Integer year, Integer oldYear) {
        GreMapStatDifferenVo  resVo=new GreMapStatDifferenVo();
        //查询绿地类型数据
        ClassFetch fetch=new ClassFetch();
        fetch.setPageSize(Integer.MAX_VALUE);
        fetch.setParentId("8c793ae3-8e14-48e7-aba6-8367cc3f0037");
        DataPager<SysClassDto> classDtoPages=sysClassLogic.getClassInfoList(fetch);
        List<SysClassDto> GreClassList=classDtoPages.records;
        Collections.sort(GreClassList,new Comparator<SysClassDto>(){
            @Override
            public int compare(SysClassDto o1, SysClassDto o2) {
                Integer orderNum2 = o2.orderNum;
                Integer orderNum1 = o1.orderNum;
                return orderNum2.compareTo(orderNum1);
            }
        });
        //获取A年情况
        Map<String,Object> map= new HashMap<>();
        map.put("year",year);
        List<GreMapStat> yearGreStats=greMapStatService.search(map);
        //获取B年情况
        map= new HashMap<>();
        map.put("year",oldYear);
        List<GreMapStat> oldYearGreStats=greMapStatService.search(map);
        //获取空间数据差异增加/减少
//        List<Geometry> yearGeoms=yearGreStats.stream().filter(f->f.getGeom()!=null).map(m->m.getGeom()).collect(Collectors.toList());
//        List<Geometry> oldYearGeoms=oldYearGreStats.stream().filter(f->f.getGeom()!=null).map(m->m.getGeom()).collect(Collectors.toList());
//        Geometry [] polygons=yearGeoms.toArray(new Geometry[yearGeoms.size()]);
//        Geometry yearGeom = factory.createGeometryCollection(polygons).buffer(0);
//        Geometry [] oldPolygons=oldYearGeoms.toArray(new Geometry[oldYearGeoms.size()]);
//        Geometry oldYearGeom = factory.createGeometryCollection(oldPolygons).buffer(0);
//        Geometry addGeom=yearGeom.difference(oldYearGeom);
//        Geometry delGeom=oldYearGeom.difference(yearGeom);
//        resVo.setAddPoints(GeometryHelper.getGeometryPoint(addGeom));
//        resVo.setDelPoints(GeometryHelper.getGeometryPoint(delGeom));
        //获取A和B两个减少的详情数据
        List<Map<String,Object>> datas=new ArrayList<>();
        for(SysClassDto sysClassDto:GreClassList){
            String name=sysClassDto.name;
            GreMapStat greMapStat=yearGreStats.stream().filter(f->f.getName().equalsIgnoreCase(name)).collect(Collectors.toList()).get(0);
            GreMapStat oldGreMapStat=oldYearGreStats.stream().filter(f->f.getName().equalsIgnoreCase(name)).collect(Collectors.toList()).get(0);
            Map<String,Object> columns=new HashMap<>();
            Double add=greMapStat.getArea()-oldGreMapStat.getArea();
            Double del=oldGreMapStat.getArea()-greMapStat.getArea();
            columns.put("name",name);
            columns.put("add",add>0?add:0);
            columns.put("del",del>0?del:0);
            datas.add(columns);
        }
        resVo.setDatas(datas);
        return JsonResult.ok(resVo);
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreMapStatVO>> listPage(@RequestBody GreMapStatQuery query) {
        return JsonResult.ok(greMapStatLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreMapStatDTO dto) {
        greMapStatLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, GreMapStat.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreMapStatVO> getInfoById(@RequestParam Integer id) {
        GreMapStat entity = greMapStatLogic.getEntity(id);
        GreMapStatVO vo= mapper.map(entity, GreMapStatVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greMapStatLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(GreMapStatQuery query, HttpServletResponse response) {
        List<GreMapStatVO> list = greMapStatLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", GreMapStatVO.class, "", response);
    }

}
